package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.ReturnDTO;
import com.example.dto.SOrderDTO;
import com.example.dto.SOrderQueryDTO;
import com.example.entity.SOrder;
import com.example.service.SOrderService;
import com.example.vo.SOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sOrder")
@Slf4j
/*
  订单管理
  订单不存在删除，只提供增加,查询,修改状态
 */
public class SOrderController {

    @Autowired
    private SOrderService sOrderService;

    /**
     * 查询订单详情 pass
     *
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public Result<SOrder> getSOrder(@PathVariable Long orderId) {
        log.info("查询订单详情{}", orderId);
        SOrderVO sOrder = sOrderService.getSOderDetail(orderId);
        return Result.success(sOrder);
    }

    /**
     * 创建订单 pass 单条
     *
     * @param orderDTO
     * @return
     */
    @PostMapping
    //@Transactional(rollbackFor = Exception.class)
    public Result submitSOrder(@RequestBody SOrderDTO orderDTO) {
        log.info("创建订单{}", orderDTO);
        sOrderService.submitSOrder(orderDTO);
        return Result.success();
    }

    /**
     * 创建订单 pass 批量，也就是一个购物车的全部
     *
     * @param
     * @return
     */
    @PostMapping("/submit")
    public Result<?> submitSOrderBatch(@RequestBody List<SOrder> sOrders) {
        log.info("批量创建订单{}", sOrders);
        List<SOrder> sOrderList = sOrderService.submitSOrderBatch(sOrders);

        return Result.success(sOrderList);
    }

    /**
     * 更改订单状态 pass
     *
     * @param orderId 订单id
     * @param state   订单状态
     * @return
     */
    @PutMapping
    public Result<?> updateState(@RequestParam Integer state, @RequestParam Long orderId) {
        log.info("更改订单状态{}:{}", orderId, state);
        String s = sOrderService.updateState(orderId, state);
        return Result.success(s);

    }

    /**
     * 模糊查询订单列表 pass
     *
     * @param sOrderQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<SOrderVO>> page(@RequestBody SOrderQueryDTO sOrderQueryDTO) {
        log.info("查询订单列表模糊pageNum:{},pageSize:{},orderId:{},startTime:{},endTime:{},state:{},userId:{}",
                sOrderQueryDTO.getPageNum(),
                sOrderQueryDTO.getPageSize(),
                sOrderQueryDTO.getOrderId(),
                sOrderQueryDTO.getStartTime(),
                sOrderQueryDTO.getEndTime(),
                sOrderQueryDTO.getState(),
                sOrderQueryDTO.getUserId());
        PageResult<SOrderVO> orderPage = sOrderService.pageOrder(
                sOrderQueryDTO.getPageNum(),
                sOrderQueryDTO.getPageSize(),
                sOrderQueryDTO.getOrderId(),
                sOrderQueryDTO.getStartTime(),
                sOrderQueryDTO.getEndTime(),
                sOrderQueryDTO.getState(),
                sOrderQueryDTO.getUserId());
        return Result.success(orderPage);
    }

    /**
     * 统计每种状态订单数量
     *
     * @return
     */
    @GetMapping("/count")
    public Result<Map<String, Long>> orderCount(@RequestParam(required = false) Long userId) {
        log.info("统计每种状态订单数量,userId:{}", userId);
        Map<String, Long> countMap = sOrderService.orderCount(userId);
        return Result.success(countMap);
    }

    @DeleteMapping("/{orderId}")
    public Result<?> cancelOrder(@PathVariable Long orderId) {
        log.info("取消订单{}", orderId);
        sOrderService.cancelOrder(orderId);
        return Result.success();
    }
    @PutMapping("/return")
    public Result<?> returnSOrder(@RequestBody ReturnDTO returnDTO){
        log.info("退货{}",returnDTO);
        sOrderService.returnSOrder(returnDTO.getOrderId(),returnDTO.getReason());
        return Result.success();
    }
}
